module.exports = Behavior({
  properties: {
    des: {
      type: Object,
      value: {},
      observer: function (o) {
        if (o && o.img.length) {
          for (var e = [], i = [], t = 0; t < o.img.length; t++) {
            var n = o.img[t];
            'image' === n.type && e.push(n.src),
              'video' === n.type && i.push(n.src);
          }
          this.setData({
            _imgList: e,
            _videoList: i,
            _needDownTimes_img: e.length,
            _needDownTimes_video: i.length,
            _needDownAllTimes: i.length + e.length,
          });
        }
      },
    },
  },
  data: {
    _imgList: [],
    _videoList: [],
    _downNowTimes: 1,
    _needDownAllTimes: 999,
    _needDownTimes_img: 999,
    _needDownTimes_video: 999,
    _wasDown_img: 0,
    _wasDown_video: 0,
  },
  methods: {
    _down: function () {
      var o = this.data,
        e = o._needDownAllTimes,
        i = o._downNowTimes,
        t = o._needDownTimes_img,
        n = o._needDownTimes_video;
      if (0 === t && 0 === n)
        return (
          wx.showToast({ title: '下载完成', icon: 'none', duration: 1200 }),
          void ('function' == typeof this.downOver && this.downOver())
        );
      wx.showLoading({
        title: '正在下载'.concat(i, '/').concat(e),
        icon: 'loading',
      }),
        t > 0
          ? (console.log('_needDownTimes_img > 0'), this._downImg())
          : n > 0 &&
            (console.log('_needDownTimes_video > 0'), this._downVideo());
    },
    _downVideo: function () {
      var o = this,
        e = this.data,
        i = e._videoList,
        t = e._downNowTimes,
        n = e._needDownTimes_video,
        s = e._wasDown_video;
      wx.downloadFile({
        url: i[s],
        success: function (e) {
          wx.saveVideoToPhotosAlbum({
            filePath: e.tempFilePath,
            success: function () {
              (o.data._wasDown_video = s + 1),
                (o.data._needDownTimes_video = n - 1),
                (o.data._downNowTimes = t + 1),
                o._down();
            },
            fail: function (o) {
              wx.hideLoading();
            },
          });
        },
        fail: function (o) {
          wx.hideLoading();
        },
      });
    },
    _downImg: function () {
      var o = this,
        e = this.data,
        i = e._imgList,
        t = e._downNowTimes,
        n = e._needDownTimes_img,
        s = e._wasDown_img;
      wx.downloadFile({
        url: i[s],
        success: function (e) {
          wx.saveImageToPhotosAlbum({
            filePath: e.tempFilePath,
            success: function () {
              (o.data._wasDown_img = s + 1),
                (o.data._needDownTimes_img = n - 1),
                (o.data._downNowTimes = t + 1),
                o._down();
            },
            fail: function (o) {
              wx.hideLoading();
            },
          });
        },
        fail: function (o) {
          wx.hideLoading();
        },
      });
    },
    _viewImg: function (o) {
      wx.previewImage({
        current: o.currentTarget.dataset.src,
        urls: this.data._imgList,
      });
    },
    _hackB: function () {},
    _copy: function (o) {
      var e = this.data.des,
        i = e.emoji_content,
        t = e.content;
      wx.setClipboardData({ data: i || t });
    },
    _beforDown: function () {
      var o = this;
      wx.getSetting({
        success: function (e) {
          JSON.stringify(e.authSetting).includes('scope.writePhotosAlbum')
            ? !e.authSetting['scope.writePhotosAlbum'] &&
              JSON.stringify(e.authSetting).includes('scope.writePhotosAlbum')
              ? (wx.showToast({ title: '打开授权才能保存', icon: 'none' }),
                wx.openSetting())
              : (o._down(), o._postPoint())
            : (wx.showToast({ title: '请同意授权!', icon: 'none' }),
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success: function () {
                  o._down(), o._postPoint();
                },
                fail: function (o) {
                  console.error('authorize 授权失败', o),
                    wx.showToast({ title: '授权失败', icon: 'none' });
                },
              }));
        },
      });
    },
    _postPoint: function () {
      if (this.data.des.id)
        try {
          getApp()
            .$api.goods.statShare({ id: this.data.des.id, type: 1 })
            .then(function (o) {
              console.log('app.$api.goods.statShare: ', o);
            });
        } catch (o) {
          console.error('统计下载量 error: ', o);
        }
    },
  },
});
